<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    $inputSerialNumberStrArray = $form->get('inputSerialNumberStr')->getValue();
    $sendWarehouseArray = $form->get('sendWarehouse')->getValue();
    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dberp-menu-top">
            <a href="<?php echo $this->url('sales-order');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回销售订单列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('点击发货出库'); ?></button>
            <?php echo $this->HelpUrl('salesSendOrder'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo $this->translate('订单发货出库');?></h3>
                    </div>

                    <div class="box-body">
                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('销售单号'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getSalesOrderSn());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('订单状态'); ?>：</b><?php echo $this->salesHelper()->salesOrderState($this->orderInfo->getSalesOrderState(), 2);?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('付款方式'); ?>：</b><?php echo $this->salesHelper()->orderReceivables($this->orderInfo->getReceivablesCode());?>
                                </div>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getOneCustomer()->getCustomerName());?>
                                </div>
                            </div>
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户联系人'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getCustomerContacts());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户手机'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getCustomerPhone());?>
                                </div>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户座机'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getCustomerTelephone());?>
                                </div>
                            </div>
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('收货地址'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getCustomerAddress());?>
                                </div>
                            </div>
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('备注'); ?>：</b><?php echo $this->escapeHtml($this->orderInfo->getSalesOrderInfo());?>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <?php echo $this->translate('发货单号'); ?><span class="must_add_value">*</span>
                                    <?php echo $this->formElement($form->get('sendOrderSn')); ?>
                                    <?php echo $this->formElementErrors($form->get('sendOrderSn'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        </div>
                        <div class="box-header">
                            <h4 class="box-title"><?php echo $this->translate('发货商品'); ?></h4>
                            <?php echo $this->formElementErrors($form->get('sendWarehouse'), ['class'=>'error-message']); ?>
                            <?php echo $this->formElementErrors($form->get('inputSerialNumberStr'), ['class'=>'error-message']); ?>
                        </div>
                        <div class="box-body table-responsive no-padding">
                            <div style="padding-bottom: 10px;">
                                <label class="checkbox-inline text-bold" style="padding-left: 0px;"><?php echo $this->translate('批量选择仓库'); ?></label>
                                <?php if (!empty($this->warehouseList)) foreach ($this->warehouseList as $warehouseId => $warehouseName) { ?>
                                    <label class="checkbox-inline"><input type="checkbox" name="selectWarehouseId[]" id="selectWarehouseId_<?php echo $warehouseId; ?>" onclick="selectWarehouse(<?php echo $warehouseId; ?>)" value="<?php echo $warehouseId; ?>"> <?php echo $warehouseName; ?></label>
                                <?php } ?>
                            </div>
                            <table class="table">
                                <tr>
                                    <th width="15%"><?php echo $this->translate('商品编号'); ?></th>
                                    <th width="20%"><?php echo $this->translate('商品名称'); ?></th>
                                    <th><?php echo $this->translate('商品规格'); ?></th>
                                    <th><?php echo $this->translate('单位'); ?></th>
                                    <th><?php echo $this->translate('销售单价'); ?></th>
                                    <th><?php echo $this->translate('销售数量'); ?></th>
                                    <th><?php echo $this->translate('税金'); ?></th>
                                    <th width="10%"><?php echo $this->translate('销售总价'); ?></th>
                                </tr>

                                <?php
                                foreach ($this->orderGoods as $goodsValue) {
                                    ?>
                                    <tr>
                                        <td><?php echo $this->escapeHtml($goodsValue->getGoodsNumber());?></td>
                                        <td>
                                            <?php echo $this->escapeHtml($goodsValue->getGoodsName());?>
                                            <?php if ($goodsValue->getGoodsSerialNumberState() == 1) { ?>
                                                <span class="badge bg-green"><?php echo $this->translate('序列号'); ?></span>
                                            <?php } ?>
                                        </td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getGoodsSpec());?></td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getGoodsUnit());?></td>
                                        <td><?php echo $this->erpCurrencyFormat($goodsValue->getSalesGoodsPrice());?></td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getSalesGoodsSellNum());?></td>
                                        <td><?php echo floatval($goodsValue->getSalesGoodsTax());?>%</td>
                                        <td><?php echo $this->erpCurrencyFormat($goodsValue->getSalesGoodsAmount());?></td>
                                    </tr>
                                    <?php if($this->warehouse[$goodsValue->getGoodsId()]) { ?>
                                    <tr>
                                        <td colspan="8">
                                            <label class="checkbox-inline text-bold" style="padding-left: 0px;"><?php echo $this->translate('选择发货仓库'); ?>：</label>
                                            <?php foreach ($this->warehouse[$goodsValue->getGoodsId()] as $warehouseValue) { if($warehouseValue->getWarehouseGoodsStock() > 0) { ?>
                                                <label class="checkbox-inline"><input type="checkbox" title="<?php echo $this->translate('请选择仓库'); ?>" required minlength="1" <?php if (isset($sendWarehouseArray[$goodsValue->getGoodsId()]) && in_array($warehouseValue->getWarehouseId(), $sendWarehouseArray[$goodsValue->getGoodsId()])) echo 'checked'; ?> name="sendWarehouse[<?php echo $goodsValue->getGoodsId();?>][]" class="sendWarehouse_<?php echo $warehouseValue->getWarehouseId();?>" value="<?php echo $warehouseValue->getWarehouseId();?>"> <?php echo $warehouseValue->getOneWarehouse()->getWarehouseName(); ?>(<strong><?php echo $this->translate('库存：').$warehouseValue->getWarehouseGoodsStock(); ?></strong>)</label>
                                            <?php } } ?>

                                            <?php if ($goodsValue->getGoodsSerialNumberState() == 1) { ?>
                                                <div>
                                                    <input type="hidden" name="inputSerialNumberStr[<?php echo $goodsValue->getGoodsId();?>]" id="inputSerialNumberStr<?php echo $goodsValue->getGoodsId();?>" value="<?php if (!empty($inputSerialNumberStrArray[$goodsValue->getGoodsId()])) echo $inputSerialNumberStrArray[$goodsValue->getGoodsId()]; ?>">
                                                    <div class="text-sm" id="goodsSerialNumberStr<?php echo $goodsValue->getGoodsId();?>"><?php if (!empty($inputSerialNumberStrArray[$goodsValue->getGoodsId()])) echo $inputSerialNumberStrArray[$goodsValue->getGoodsId()]; ?></div>
                                                    <button type="button" onclick="selectGoodsSerialNumber(<?php echo $goodsValue->getGoodsId();?>, <?php echo $goodsValue->getSalesGoodsSellNum(); ?>);" class="btn btn-success btn-xs col-xs-4"><?php echo $this->translate('点击手动选择序列号 ( 可不选，系统随机发货 )'); ?></button>
                                                </div>
                                            <?php } ?>
                                        </td>
                                    </tr>
                                    <?php } else { ?>
                                    <tr>
                                        <td colspan="8">
                                            <label class="checkbox-inline text-bold" style="padding-left: 0px;"><?php echo $this->translate('选择发货仓库'); ?>：</label>
                                            <label class="text-danger"><?php echo $this->translate('仓库无库存'); ?></label>
                                        </td>
                                    </tr>
                                    <?php } ?>
                                <?php } ?>

                                <tr id="tr-end">
                                    <td colspan="6"></td>
                                    <td colspan="2" class="text-right"><b><?php echo $this->translate('销售金额合计'); ?>：</b><span id="order-amount"><?php echo (isset($orderInfo) ? $this->erpCurrencyFormat($orderInfo->getSalesOrderAmount()) : 0); ?></span></td>
                                </tr>
                            </table>
                        </div>

                    </div>
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary col-md-offset-5"><i class="fa fa-check"></i> <?php echo $this->translate('点击发货出库'); ?></button>
                    </div>
                </div>


            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>

    <div class="modal fade" id="modalDefault">
        <div class="modal-dialog" style="width: 600px;" id="addGoodsSerialNumberBody">

        </div>
    </div>

</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate();
    });

    function selectWarehouse(warehouseId) {
        let warehouse = $('#selectWarehouseId_'+warehouseId);
        if (warehouse.is(':checked')) {
            $(".sendWarehouse_"+warehouse.val()).prop("checked", true);
        } else {
            $(".sendWarehouse_"+warehouse.val()).prop("checked", false);
        }
    }

    function selectGoodsSerialNumber(goodsId, serialNumberN) {
        let sendWarehouseIdStr = '';
        $("input[name='sendWarehouse["+goodsId+"][]']").each(function () {
            if(this.checked) {
                if (sendWarehouseIdStr == '') sendWarehouseIdStr = this.value;
                else sendWarehouseIdStr += ',' + this.value;
            }
        });

        if (sendWarehouseIdStr == '') {
            erpMessage('<?php echo $this->translate('请选择发货仓库'); ?>');
            return false;
        }

        $('#modalDefault').modal('show');

        $.post("<?php echo $this->url('sales-order', ['action' => 'ajaxSendGoodsSerialNumberStr']);?>", {sendWarehouseIdStr: sendWarehouseIdStr, goodsId:goodsId, serialNumberN:serialNumberN, inputSerialNumberStr:$('#inputSerialNumberStr'+goodsId).val()}, function (html) {
            $('#addGoodsSerialNumberBody').html(html);
        });
    }

    function hiddenSelectGoodsSerialNumber() {
        $('#modalDefault').modal('hide');
    }
</script>